package com.liao.domain.strategy.service.rule.chain.impl;

import com.liao.domain.strategy.repository.IStrategyRepository;
import com.liao.domain.strategy.service.rule.chain.AbstractLogicChain;
import com.liao.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import com.liao.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author long
 * @Description 白名单
 * @Date 2024-08-26 11:38
 */
@Slf4j
@Component("rule_whitelist")
public class WhiteListLogicChain extends AbstractLogicChain {
    @Resource
    private IStrategyRepository strategyRepository;

    @Override
    public DefaultChainFactory.StrategyAwardVO logic(String userId, Long strategyId) {
        log.info("抽奖责任链-白名单开始 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        //获取黑名单的规则 数据结构：奖品ID:用户ID1,用户ID2,用户ID3
        String ruleValue = strategyRepository.queryStrategyRuleValue(strategyId, ruleModel());
        String[] ruleValueSplit = ruleValue.split(Constants.COLON);
        Long awardId = Long.parseLong(ruleValueSplit[0]);
        String[] userBlackList = ruleValueSplit[1].split(Constants.SPLIT);
        for (String userBlack : userBlackList) {
            if (userId.equals(userBlack)) {
                log.info("抽奖责任链-白名单接管 userId: {} strategyId: {} ruleModel: {} awardId: {}", userId, strategyId, ruleModel(), awardId);
                return DefaultChainFactory.StrategyAwardVO.builder()
                        .awardId(awardId)
                        .logicModel(ruleModel())
                        .build();
            }
        }
        // 过滤其他责任链
        log.info("抽奖责任链-白名单放行 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        return super.next().logic(userId, strategyId);
    }

    @Override
    protected String ruleModel() {
        return DefaultChainFactory.LogicModel.RULE_WHITELIST.getCode();
    }
}
